package com.demo.common.job;

import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.demo.common.model.Proxyip;
import com.demo.webmagic.pageProcessor.PageProcessorValiProxyIp;
import com.demo.webmagic.pipeline.PipelineValiProxyIp;
import com.jfinal.log.Log;

import us.codecraft.webmagic.Spider;


public class JobCheckProxyIp implements Job {

	private static final Log log = Log.getLog(JobCheckProxyIp.class);
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		log.debug("验证代理ip任务开始...");
		List<Proxyip> proxyipList = Proxyip.dao.find("SELECT * FROM proxyip");
        //获取数据库中的代理信息，无效的删除，有效的设置为无效
        for (Proxyip proxyip : proxyipList) {
    		proxyip.setIsVali(0);
			proxyip.update();
		}
        int i=0;
        for (final Proxyip proxyip : proxyipList) {
        	Spider.create(new PageProcessorValiProxyIp(proxyip)).addPipeline(new PipelineValiProxyIp()).addUrl("http://www.ip138.com/ip2city.asp").start();
        	try {
				Thread.sleep(100);
				System.out.println("验证第" + i++ + "个ip");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
